package com.corex.pinmethods;

import com.corex.staticclasses.PrivateData;
import com.corex.utils.Constants;
import com.corex.vinayak.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class SetPinActivity extends Activity implements View.OnClickListener{
	public static int passwordArray[] = new int[]{R.id.clicked_pass1,R.id.clicked_pass2,R.id.clicked_pass3,R.id.clicked_pass4};
	public String pass_pressed = "";
	private String pin_first;
	private String pin_confirm;
	private TextView pinLabel;
    private Button exitButton;

    @Override
    public void onBackPressed() {

    }

    @Override
	protected void onCreate(Bundle savedInstanceState) {
		final View view;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lockscreen);

		view = findViewById(R.id.lockscreenLayout);
		pinLabel = (TextView) findViewById(R.id.pin_label);
		getActionBar().hide();
	}

	@Override
	public void onClick(View v) {

        String pinScreenType = null;

		if(null != getIntent().getExtras()){
			pinScreenType = getIntent().getExtras().getString(Constants.PIN_SCREEN_TYPE);
		}
		if(null!=pinScreenType && "CheckPin".equals(pinScreenType)){
			pass_pressed = getPassPressed(v);
			if(null!=pass_pressed && pass_pressed.length()==4){

				PrivateData privateData = new PrivateData(getApplicationContext());
				if(pass_pressed.equals(privateData.getPin())){
					Intent returnIntent = new Intent();
                    returnIntent.putExtra(Constants.IS_PIN_CORRECT,true);
                    setResult(RESULT_OK,returnIntent);
                    finish();

				}else{
					Toast.makeText(getApplicationContext(), "Incorrect Pin", Toast.LENGTH_SHORT).show();
					resetPinData();
				}
			}
		}else{
			pass_pressed = getPassPressed(v);


		if(pass_pressed.length()==4){
			if(null==pin_first || pin_first.length()==0){
				pin_first = pass_pressed;
				pass_pressed = "";
				resetPinEnter();
				pinLabel.setText("Re-Enter PIN");
			}else{
				pin_confirm = pass_pressed;

				if(!pin_first.equals(pin_confirm)){
					pinLabel.setText("Wrong PIN Confirmation");
					pinLabel.setTextColor(Color.RED);
					resetPinData();
					View view = findViewById(R.id.lockscreenLayout);
					Animation shake = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.shake);
					view.startAnimation(shake);
					int index = 0;
					while(index<4){
						Button passArrayButton = (Button) findViewById(passwordArray[index]);
						passArrayButton.setBackgroundResource(R.drawable.circular);
						index++;
					}
				}else{
					Toast.makeText(getApplicationContext(), "Pin Saved", Toast.LENGTH_SHORT).show();
					PrivateData privateData = new PrivateData(getApplicationContext());
					privateData.setPin(pin_first);
					Intent intent = new Intent(SetPinActivity.this,com.corex.navigationdrawer.LoginSuccessActivity.class);
					startActivity(intent);
					finish();
				}
			}
		}
		}
	}

	private boolean checkLogin(String pin){
		return (pin.equals("1234"));
	}

	private void resetPinData(){
		pin_first = "";
		pin_confirm = "";
		pass_pressed = "";
		resetPinEnter();
	}

	private void resetPinEnter(){
		int index = 0;
		while(index<4){
			Button passArrayButton = (Button) findViewById(passwordArray[index]);
			passArrayButton.setBackgroundResource(R.drawable.circular);
			index++;
		}
	}

	private String getPassPressed(View v){
		Button button = (Button) v;
		String btnText = button.getText().toString();
		if(!"Back".equals(btnText) && !"Cancel".equals(btnText)){
			pinLabel.setTextColor(Color.WHITE);
			pinLabel.setText("Enter PIN");
			if(pass_pressed.length()<4){
				pass_pressed+=btnText;
			}
		}else{
			if("Back".equals(btnText)){
				if(null!=pass_pressed && pass_pressed.length()>0){
					String editedString = pass_pressed.substring(0, pass_pressed.length()-1);
					pass_pressed = editedString;
				}
			}else if("Cancel".equals(btnText)){
					View view = findViewById(R.id.lockscreenLayout);
					Animation shake = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.shake);
					view.startAnimation(shake);

                new AlertDialog.Builder(SetPinActivity.this)
                        .setIcon(android.R.drawable.ic_dialog_alert)
                        .setTitle(R.string.title_exit)
                        .setMessage(R.string.exit_app_msg)
                        .setPositiveButton(R.string.yes,
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog,
                                                        int which) {
                                        SetPinActivity.this.finish();
                                    }
                                }).setNegativeButton(R.string.no, null).show();
			}
		}

		int pass_length = pass_pressed.length();
		int index = 0;

		while(index<4){
			Button passArrayButton = (Button) findViewById(passwordArray[index]);
			passArrayButton.setBackgroundResource(R.drawable.circular);
			index++;
		}

		index = 0;
		while(index<pass_length){
			Button passArrayButton = (Button) findViewById(passwordArray[index]);
			passArrayButton.setBackgroundResource(R.drawable.circular_filled);
			index++;
		}
		return pass_pressed;
	}
}
